<template>
	<view class="content">
		<view class="box">
			<image :src="userInfo.avatar?userInfo.avatar:'/static/logo.png'" style="width: 200upx;height: 200upx;border-radius: 50%;"
			 @click="handleupdateImg"></image>
		</view>
		<view class="line">
			<span class="test">昵称</span>
			<input class="username" v-model="userInfo.nickname" style="margin-left: 30upx;"></input>
		</view>
		<view class="line">
			<text class="test">性别</text>
			<radio-group v-model="userInfo.gender" style="margin-left: 30upx;display: flex;" @change="radioChange">
				<label style="margin-right: 20upx;display: flex;" v-for="(item,index) in items" :key="index">
					<view>
						<radio :value="item.value" :checked="index === current" />
					</view>
					<view>{{item.name}}</view>
				</label>
			</radio-group>
		</view>
		<view class="line">
			<text class="test">电话</text>
			<input class="username" v-model="userInfo.mobile" style="margin-left: 30upx;"></input>
		</view>
		<view class="line">
			<text class="test">邮箱</text>
			<input class="username" v-model="userInfo.email" style="margin-left: 30upx;"></input>
		</view>
		<button type="primary" class="login-btn" @click="updateUserInfo">提交</button>
	</view>
</template>

<script>
	import {
		get,
		post,
		showToast
	} from '../../js/common';
	export default {
		data() {
			return {
				items: [{
						value: '0',
						name: '男'
					},
					{
						value: '1',
						name: '女',
					}
				],
				current: 0,
				avatar: '',
				userId: '',
				images: {},
				userInfo: {},
			}
		},
		onShow() {
			this.getuserinfo()
		},
		methods: {
			getuserinfo() {
				try {
					this.userId = uni.getStorageSync('userId')
					const params = {
						userId: this.userId
					}
					post('mobileapi/mobile/member/usercenter/showUserInfo', params).then(res => {
						this.userInfo = res.userinfo.userInfo
						this.userInfo.email = res.userinfo.userInfo.email == 'null'? '':res.userinfo.userInfo.email
						if(this.userInfo.gender){
							this.userInfo.gender = this.userInfo.gender
							this.current = this.userInfo.gender
						}else{
							this.userInfo.gender = 0
						}
					})
				} catch (e) {}
			},
			radioChange(e){
				this.userInfo.gender = e.detail.value
			},
			updateUserInfo() {
				try {
					this.userId = uni.getStorageSync('userId')
					const params = {
						id: this.userId,
						avatar: this.images.id,
						nickname: this.userInfo.nickname,
						gender: this.userInfo.gender,
						email: this.userInfo.email,
						mobile: this.userInfo.mobile
					}
					post('mobileapi/mobile/member/usercenter/updateUserInfo', params).then(res => {
						if (res.code === 0) {
							uni.switchTab({
								url: '/pages/me/index'
							})
						}
					})
				} catch (e) {}
			},
			handleupdateImg() {
				var that = this
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: 'http://yingyuphone.yuzhekeji.top:9002/mobileapi/mobile/member/usercenter/uploadPic?token=' + uni.getStorageSync(
								'token'),
							// url: 'http://192.168.10.43:9002/mobileapi/mobile/member/usercenter/uploadPic?token=' + uni.getStorageSync(
							// 	'token'),
							filePath: tempFilePaths[0],
							name: 'file',
							success: (res) => {
								if (res.statusCode == 200) {
									var res = JSON.parse(res.data)
									that.images = res.ossEntity
									const params = {
										id: that.userId,
										avatar: that.images.id
									}
									post('mobileapi/mobile/member/usercenter/updateUserAvatar', params).then(res => {
										uni.showToast({
											title: res.msg
										})
										this.getuserinfo()
									})
								}
							}
						})
					}
				});
			},
			async saveUserInfo() {
				var param = {}
				if (this.imgUrl) {
					param.img = this.imgUrl
				}
				let res = await this.$api.userInfoUpdata(param)
				res = res.data
				if (res.success) {
					uni.showToast({
						title: '修改成功'
					})
				}
			},
		}
	}
</script>

<style>
	.content {
		font-size: 28upx;
	}

	.box {
		margin: 100upx auto;
		text-align: center;
	}

	.line {
		display: flex;
		padding: 20upx;
		margin: 0 20upx 1px 20upx;
		background-color: #FFFFFF;
	}

	.test {
		margin: 10upx 0 0 30upx;
	}

	.txt {
		margin: 25upx 0 0 20upx;
		font-size: 40upx;
	}

	.radio {
		margin: 0 auto;
	}

	.login-btn {
		margin: 60upx 20upx 0 20upx;
		background: #D7000F;
	}
</style>
